import {defineStore} from 'pinia'
import {message} from 'ant-design-vue';
import dayjs from "dayjs";

import zjRequest from "@/services";
import {IResponseData} from "@/services/types";
import {IActions, IListItem, IState, IUserDetailParam, USER_URL_TYPE} from "@/store/userModule/types";
import {USER_LIST_DEFAULT_PAGE_SIZE} from "@/constants/data";

const useUserStore = defineStore<any, IState, any, IActions>('userModule', {
    state: () => {
        return {
            userInfo: {
                list: [],
                page_info: {
                    current_page: 1,
                    page_size: 10,
                    total_count: 0
                }
            },
            currentUserDetail: {
                "id": 0,
                "name": "",
                "password": "",
                "age": 0,
                "gender": 1,
                "salary": 0,
                "create_time": dayjs().format("YYYY-MM-DD HH:mm:ss"),
                "depart_id": 0
            }
        }
    },
    actions: {
        // 获取用户详情
        async requestUserDetailAction(params: IUserDetailParam) {
            const res: IResponseData = await zjRequest.get({
                url: USER_URL_TYPE.USER_DETAIL,
                params
            })
            if (res.code === 0) {
                this.currentUserDetail = res.data
            }
        },
        // 获取用户列表
        async requestUserListAction({current_page, page_size}) {
            const res: IResponseData = await zjRequest.get({
                url: USER_URL_TYPE.USER_LIST,
                params: {
                    current_page,
                    page_size
                }
            })
            if (res.code === 0) {
                this.userInfo = res.data
                this.userInfo.list = res.data.list.map((item: IListItem) => {
                    item.create_time = dayjs(item.create_time).format("YYYY-MM-DD HH:mm:ss")
                    return item
                })
            }
        },

        // async addUserAction(params) {
        //   const res: IResponseData = await zjRequest.post({
        //     url: USER_URL_TYPE.USER_ADD,
        //     data: params
        //   })
        //   return res.code === 0;
        // }

        async addUserAction(params) {
            const res: IResponseData = await zjRequest.post({
                url: USER_URL_TYPE.USER_ADD,
                data: params
            })
            if (res.code === 0) {
                message.success("添加成功")
                this.requestUserListAction({
                    page_size: USER_LIST_DEFAULT_PAGE_SIZE,
                    current_page: 1
                })
                return true
            } else {
                return false
            }
        },

        async deleteUserAction(params) {
            const res: IResponseData = await zjRequest.post({
                url: USER_URL_TYPE.USER_DELETE,
                data: params
            })
            if (res.code === 0) {
                message.success("删除成功")
                this.requestUserListAction({
                    current_page: 1,
                    page_size: USER_LIST_DEFAULT_PAGE_SIZE
                })
                return true
            } else {
                return false
            }
        },
        async updateUserAction(params) {
            const res: IResponseData = await zjRequest.post({
                url: USER_URL_TYPE.USER_UPDATE,
                data: params
            })
            if (res.code === 0) {
                message.success("修改成功")
                this.requestUserListAction({
                    current_page: this.userInfo.page_info.current_page,
                    page_size: this.userInfo.page_info.page_size,
                })
                return true
            } else {
                return false
            }
        }
    }
})
export default useUserStore
